<template>
  <div>
    <div class="mask" @click="goClose">
      <slot></slot>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    showMask: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      isClose: true,
    };
  },
  watch: {
    showMask: {
      handler(val) {
        this.getZIndex();
      },
    },
  },
  methods: {
    goClose() {
      this.isClose = !this.isClose;
      this.$emit("close", this.isClose);
    },
    getZIndex() {
      const element = document.querySelector(".mask");
      // 给元素设置样式属性值
      if (this.showMask) {
        element.style.setProperty("--z-index", 99999);
      } else {
        element.style.setProperty("--z-index", -1);
      }
    },
  },
};
</script>

<style scoped lang="scss">
.mask {
  position: relative;
  --z-index: -1;
}

.mask::before {
  content: "";
  display: block;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  position: absolute;
  top: 0;
  left: 0;
  z-index: var(--z-index);
}
</style>